package com.spring_mysql_redis.timing;

import com.spring_mysql_redis.service.UserLikesService;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.text.SimpleDateFormat;
import java.util.Date;

public class LikeTask extends QuartzJobBean {

    @Autowired
    UserLikesService userLikesService;

    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {

        //将 Redis 里的点赞信息同步到数据库里
        String save = userLikesService.saveData();
        if(save.equals("SUCCESS")){
            System.out.println("LikedCountFromRedis方法--------------------------------------------------------------------");
            userLikesService.delLikedDataFromRedis();
            //调用异步线程等待执行
            LikedCountFromRedis();
        }

//        likedService.transLikedFromRedis2DB();
//        likedService.transLikedCountFromRedis2DB();
    }

    @Async
    void LikedCountFromRedis(){
        try {
            Thread.currentThread().sleep(1000*60*5);
            //mysql点赞表存储完再更新动态表赞数
            System.out.println("异步执行--------------------------------------------------------------------");
            userLikesService.getLikedCountFromRedis();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}
